package com.poly.smart.car.tts_plugin;

import androidx.annotation.NonNull;

import io.flutter.embedding.engine.plugins.FlutterPlugin;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
import io.flutter.plugin.common.MethodChannel.Result;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.nfc.Tag;
import android.os.Handler;
import android.os.Looper;
import android.text.TextUtils;

import androidx.annotation.NonNull;

import org.w3c.dom.Text;

import java.io.IOException;
import java.io.InputStream;

import io.flutter.Log;
import io.flutter.embedding.engine.plugins.FlutterPlugin;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
import io.flutter.plugin.common.MethodChannel.Result;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import com.iflytek.autofly.tts.ttsproxy.ITtsClientListener;
import com.iflytek.autofly.ttsaidl.ttsproxy.ITtsService;
import com.iflytek.autofly.ttsaidl.ttsproxy.TTSPlayerConfigConstants;
import com.iflytek.autofly.ttsaidl.ttsproxy.TtsPlayerConfigEnum;
import com.iflytek.autofly.ttsaidl.ttsproxy.TtsServiceAgent;


/** TtsPlugin */
public class TtsPlugin implements FlutterPlugin, MethodCallHandler {
  /// The MethodChannel that will the communication between Flutter and native Android
  ///
  /// This local reference serves to register the plugin with the Flutter Engine and unregister it
  /// when the Flutter Engine is detached from the Activity
  private MethodChannel channel;

  private Context applicationContext;

  private final String TTS_SERVER_PACKAGE = "com.iflytek.cutefly.speechclient.hmi";

  private ITtsService defaultTtsService = null;
  @Override
  public void onAttachedToEngine(@NonNull FlutterPluginBinding flutterPluginBinding) {
    channel = new MethodChannel(flutterPluginBinding.getBinaryMessenger(), "tts_plugin");
    channel.setMethodCallHandler(this);
    applicationContext =  flutterPluginBinding.getApplicationContext();
    if (null == defaultTtsService){
      defaultTtsService = TtsServiceAgent.getInstance();
      defaultTtsService.initService((state, errId) -> {

      },applicationContext, TTSPlayerConfigConstants.VR_USAGE, TTS_SERVER_PACKAGE);
    }
  }

  @Override
  public void onMethodCall(@NonNull MethodCall call, @NonNull Result result) {
    if (TextUtils.equals("uiot.tts",call.method)){
      String text = call.argument("text");
      try {
        defaultTtsService.startSpeak(text, null, false, null,
                TtsPlayerConfigEnum.NEED_AUDIO_FOCUS_VR_CONFIG);
      } catch (Exception e) {
        System.err.println("onMethodCall  error==="+e.getLocalizedMessage());
      }
    } else {
      result.notImplemented();
    }
  }

  @Override
  public void onDetachedFromEngine(@NonNull FlutterPluginBinding binding) {
    channel.setMethodCallHandler(null);
  }
}
